﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime;
using System.Reflection;

namespace PayloadTracker
{
    public delegate void OnNewProviderDelegate(APRS.IAPRSProvider provider);
    public partial class NewProvider : Form
    {
        public event OnNewProviderDelegate OnNewProvider;


        struct APRSEntry
        {
            public string Name;
            public APRS.IAPRSProviderSetup Setup;
            public APRSEntry(string name, APRS.IAPRSProviderSetup setup)
            {
                Setup = setup;
                Name = name;
            }
            public override string ToString()
            {
                return Name;
            }
        }


        public NewProvider()
        {
            InitializeComponent();
            FindAPRSProviders();
        }

        private void FindAPRSProviders()
        {
            foreach (Type t in Assembly.GetExecutingAssembly().GetTypes())
            {
                if (t.IsAbstract)
                    continue;
                if (t.IsDefined(typeof(APRS.SetupControlAttribute), true))
                {
                    try
                    {

                        var controlattribute = t.GetCustomAttributes(typeof(APRS.SetupControlAttribute), true)[0] as APRS.SetupControlAttribute;
                        var aprsprovider = Activator.CreateInstance(t, true) as APRS.IAPRSProvider;
                        if (aprsprovider.Name != string.Empty)
                        {
                            cbProvider.Items.Add(new APRSEntry(aprsprovider.Name, Activator.CreateInstance(controlattribute.Type) as APRS.IAPRSProviderSetup));
                        }

                    }
                    catch (System.Exception ex)
                    {
                        Logger.LOG.Error("Unable to load APRSPRovider " + t.ToString() + " due to: " + ex.ToString());
                    }
                }
            }
            cbProvider.SelectedIndex = 0;
        }

        private void NewProvider_Load(object sender, EventArgs e)
        {

        }
        private void cbProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            var entry = ((APRSEntry)cbProvider.SelectedItem);
            ProviderBox.Controls.Clear();
            Point pt = ProviderBox.DisplayRectangle.Location;
            pt.X += (ProviderBox.DisplayRectangle.Width - entry.Setup.Width) / 2;
            pt.Y += (ProviderBox.DisplayRectangle.Height - entry.Setup.Height) / 2;
            entry.Setup.Anchor = AnchorStyles.None;
            entry.Setup.Location = pt;
            ProviderBox.Controls.Add(entry.Setup);

        }

        private bool ValidateControls()
        {
            try
            {
                return ((APRS.IAPRSProviderSetup)ProviderBox.Controls[0]).ValidateInput();
                
            }
            catch (System.Exception)
            {
                return false;
            }

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!ValidateControls())
            {
                MessageBox.Show("Please check your input.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            OnNewProviderDelegate handler = OnNewProvider;
            if (handler != null)
                handler(((APRS.IAPRSProviderSetup)ProviderBox.Controls[0]).CreateInstance());

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
